import { BasicColumn, FormSchema } from '/@/components/Table';
import { getDeptList } from '/@/api/backs/system/dept';
import { userSimple } from '/@/api/backs/workFlow/form';
import { getUser } from '/@/api/backs/system/user';
import moment from 'moment';

export const columns: BasicColumn[] = [
  {
    title: '员工姓名',
    dataIndex: 'name',
  },
  {
    title: '入职时间',
    dataIndex: 'hireDate',
    sorter: true,
    customRender: ({ record }) => {
      const time = record.hireDate;
      return time ? moment(time).format('YYYY-MM-DD') : '-';
    },
  },
  {
    title: '是否有试用期',
    dataIndex: 'hasProbation',
    customRender: ({ record }) => {
      if (record.infoType !== 2) {
        if (record.hasProbation) {
          return '是';
        } else {
          return '否';
        }
      } else {
        return '--';
      }
    },
  },
  {
    title: '试用工资比例',
    dataIndex: 'probationSalaryRatio',
    customRender: ({ record }) => {
      if (!record.probationSalaryRatio) {
        return '--';
      } else {
        if (record.infoType !== 2) {
          return record.probationSalaryRatio;
        } else {
          return '--';
        }
      }
    },
  },
  {
    title: '部门',
    dataIndex: 'deptName',
    customRender: ({ record }) => {
      if (record.deptName) {
        return record.deptName;
      } else {
        return '--';
      }
    },
  },
  {
    title: '基础工资',
    dataIndex: 'baseSalary',
  },
  {
    title: '身份证号码',
    dataIndex: 'identityCard',
  },
  {
    title: '人员类型',
    dataIndex: 'infoType',
    customRender: ({ record }) => {
      if (record.infoType === 1) {
        return '项目人员';
      } else if (record.infoType === 0) {
        return '部门人员';
      } else {
        return '外聘人员';
      }
    },
  },
  {
    title: '更新时间',
    dataIndex: 'updatedAt',
    customRender: ({ record }) => {
      const time = record.updatedAt;
      return time ? moment(time).format('YYYY-MM-DD') : '-';
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt',
    customRender: ({ record }) => {
      const time = record.createdAt;
      return time ? moment(time).format('YYYY-MM-DD') : '-';
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '入职时间',
    field: 'hireDate',
    component: 'RangePicker',
    colProps: { span: 8 },
    componentProps: {
      valueFormat: 'X',
    },
  },
  {
    field: 'name',
    label: '员工姓名',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: ({ tableAction }) => {
      return {
        onPressEnter: () => {
          const { reload } = tableAction;
          reload();
        },
      };
    },
  },
  {
    label: '人员类型',
    field: 'infoType',
    colProps: { span: 8 },
    component: 'Select',
    componentProps: {
      options: [
        {
          value: 0,
          label: '部门人员',
        },
        {
          value: 1,
          label: '项目人员',
        },
        {
          value: 2,
          label: '外聘人员',
        },
      ],
      getPopupContainer: () => document.body,
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '人员类型',
    field: 'infoType',
    required: true,
    component: 'Select',
    defaultValue: 0,
    componentProps: {
      allowClear: false,
      options: [
        {
          value: 0,
          label: '部门人员',
        },
        {
          value: 1,
          label: '项目人员',
        },
        {
          value: 2,
          label: '外聘人员',
        },
      ],
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'userId',
    label: '用户',
    required: true,
    component: 'ApiSelect',
    componentProps: ({ formActionType }) => {
      return {
        placeholder: '请选择用户',
        api: userSimple,
        labelField: 'nickname',
        valueField: 'id',
        labelInValue: true,
        showSearch: true,
        filterOption: (input, option) => {
          return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
        },
        onChange: async ({ value }) => {
          const { setFieldsValue, getFieldsValue } = formActionType;
          if (getFieldsValue().infoType === 0) {
            const data = await getUser({ id: value });
            setFieldsValue({
              deptId: { value: data.deptId, label: data.dept.name },
            });
          }
        },
      };
    },
  },
  {
    field: 'hireDate',
    label: '入职时间',
    required: true,
    component: 'DatePicker',
    componentProps: {
      valueFormat: 'x',
    },
  },
  {
    field: 'hasProbation',
    label: '是否有试用期',
    defaultValue: true,
    component: 'Switch',
    colProps: { span: 8 },
    required: true,
    componentProps: {
      checkedValue: true,
      unCheckedValue: false,
    },
    ifShow: ({ values }) => values.infoType !== 2,
  },
  {
    field: 'probationSalaryRatio',
    label: '试用工资比例',
    required: ({ values }) => (values.hasProbation ? true : false),
    component: 'InputNumber',
    componentProps: {
      placeholder: '请输入试用工资比例',
    },
    ifShow: ({ values }) => values.infoType !== 2,
  },
  {
    field: 'salaryPaymentMethod',
    label: '工资发放方式',
    component: 'Input',
    componentProps: {
      placeholder: '请输入工资发放方式',
    },
  },
  {
    field: 'bankAccount',
    label: '银行卡信息',
    component: 'Input',
    componentProps: {
      placeholder: '请输入银行卡信息',
    },
  },
  {
    label: '部门',
    field: 'deptId',
    required: true,
    component: 'ApiTreeSelect',
    componentProps: {
      immediate: true,
      api: () => getDeptList({}),
      replaceFields: {
        title: 'name',
        key: 'id',
        value: 'id',
      },
      handleTree: 'id',
      placeholder: '请选择部门',
      labelInValue: true,
      getPopupContainer: () => document.body,
    },
    ifShow: ({ values }) => values.infoType !== 1,
  },
  {
    field: 'baseSalary',
    label: '基础工资',
    required: true,
    component: 'InputNumber',
    componentProps: {
      placeholder: '请输入基础工资',
    },
  },
  {
    field: 'identityCard',
    required: true,
    label: '身份证号码',
    component: 'Input',
    componentProps: {
      placeholder: '请输入身份证号码',
    },
  },
];
